<template>
    <div style="padding:15px;">
        <Task ref="taskRef" :log="log" :start="start" :stop="stop" :build="build" :dir="dir" :gitPull="gitPull"/>
    </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from "vue";
import Task from "./task.vue"
import {Build,Dir,GetLog,Start,Stop, GitPull} from '../../wailsjs/go/main/App'
import { useMessage } from 'naive-ui'
const message = useMessage()

const taskRef = ref(Task)

const log = (row:string, fun?:Function) => {
    console.log("log",row)
    GetLog(row).then(res => {
        console.log("res log", res)
        fun && fun(res)
    })
}

const start = (row:string, fun?:Function) => {
    console.log("start",row)
    Start(row).then(res => {
        fun && fun(res)
    })
}

const stop = (row:string, fun?:Function) => {
    console.log("stop",row)
    Stop(row).then(res => {
        fun && fun(res)
    })
}

const build = (row:string, fun?:Function) => {
    console.log("打包开始 build前 ",row)
    Build(row).then(res => {
        console.log("打包回调中...", res)
        fun && fun(res)
    })
}

const dir = (fun?:Function) => {
    Dir().then((res:string) => {
        console.log("dir",res)
        fun && fun(res)
    })
}

const gitPull = (row:string, fun?:Function) => {
    console.log("gitPull init")
    GitPull(row).then((res:string) => {
        console.log("gitPull",res)
        fun && fun(res)
    })
}


onMounted(() => { 
  if (window.runtime && window.runtime.EventsOn) {
    console.log("log日志", window.runtime.EventsOn)
    runtime.EventsOn("log", (resStr) => {
        console.log("事件触发:", resStr) 
        const res = JSON.parse(resStr)
        console.log("res", res)
        taskRef.value.changeLog(res)
    })

    runtime.EventsOn("msg", (resStr) => {
        const res = JSON.parse(resStr)
        res.type == "success" ? message.success(res.msg) : message.error(res.msg)
    })

    runtime.EventsOn("update", (resStr) => {
        console.log("update", resStr)
        const res = JSON.parse(resStr)
        taskRef.value.changeUpdate(res)
    })
  }
}) 

</script>